package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.javaBean.StreetInfoListBean;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyToast;

import org.json.JSONObject;


/*
    意见反馈
 */

public class BackAdiceAvtivity extends BaseActivity implements ContributorView {
    private EditText edit_text_advice;
    private TextView tv_headtitle;
    private Context context;
    private MyToast myToast;
    private ContributorPresenter contributorPresenter;
    private final String MORE_SUBMIT_OPINION="MORE_SUBMIT_OPINION";
    private String cidNumber="";
    private StreetInfoListBean streetInfoListBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.back_adice_layout);
        context=this;
        contributorPresenter = new ContributorPresenter(this, context);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("意见反馈");
        myToast=new MyToast(context);
        findViewById(R.id.regist_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        edit_text_advice= (EditText) findViewById(R.id.edit_text_advice);
        findViewById(R.id.advice_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content=edit_text_advice.getText().toString();
                if (content.isEmpty()){
                    myToast.showShort("提点建议吧");
                }else {
                    String uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                    boolean isUpdate = Tools.getSharedPreferencesboolen(Constant.isUpdate, false,context);
                    String locationString = Tools.getSharedPreferencesStrings(Constant.SharePreferenceLocation, "", context);
                    if (locationString.length() > 0) {
                        streetInfoListBean = JsonTools.jsonStringToObject(locationString, StreetInfoListBean.class);
                        cidNumber=streetInfoListBean.getId();
                    }
                    String allMessage = Tools.getSharedPreferencesStrings(Constant.SharePreferenceMyModuleInfo,"",context);
                    String phone ="";
                    if(!TextUtils.isEmpty(allMessage)){
                        try {
                            JSONObject obj = new JSONObject(allMessage);
                            phone = obj.optString("phone");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    contributorPresenter.getMoreSubmitOpinion(uid,content,cidNumber,phone,MORE_SUBMIT_OPINION);
                }
            }
        });
    }
    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(MORE_SUBMIT_OPINION)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object message=respObj.getMessage();
                if (code.equals("200") && message!=null) {
                    if(message.equals("41")) {
                        myToast.showShort("反馈成功");
                        finish();
                    }
                } else {
                    myToast.showShort("操作失败");
                }
            }
        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }
}
